package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioButtonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        Button btnchange = findViewById(R.id.btnpost);
        RadioGroup radioGroup = findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = findViewById(i);
                //Toast.makeText(RadioButtonActivity.this,"你选了："+radioButton.getText(),Toast.LENGTH_SHORT).show();
                Toast toast = Toast.makeText(getApplicationContext()
                        ,"",Toast.LENGTH_SHORT);
                toast.setText("helloworld");
                toast.show();
            }
        });
        btnchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for(int i=0;i<radioGroup.getChildCount();i++){
                    RadioButton rd = (RadioButton) radioGroup.getChildAt(i);
                    if(rd.isChecked()){
                        Toast toast = Toast.makeText(RadioButtonActivity.this
                                ,null,Toast.LENGTH_SHORT);
                        toast.setText("你选择的是： "+rd.getText());
                        toast.show();
                    }
                }
            }
        });
    }
}